package com.gmrz.uap.operlog;

import com.gmrz.uap.cfg.SystemConst;
import com.gmrz.uap.common.UAPSSOHelper;
import com.gmrz.uap.model.OperLog;
import com.gmrz.uap.model.User;
import com.gmrz.uap.util.DateUtil;
import com.gmrz.uap.util.IPUtils;
import com.gmrz.uap.util.Identities;
import com.jfinal.plugin.activerecord.Page;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

public class OperLogService {
    public static final OperLogService me = new OperLogService();


    /**
     * 根据页面传过来的条件进行分页查询
     *
     * @param page  页号
     * @param rows  行数
     * @param sort  排序字段
     * @param order 升序/降序
     * @return　分页结果 Page<FidoLog>
     */
    public Page<OperLog> findOperLogListForPaginate(OperLog operLog, int page, int rows, String sort, String order, Map<String, Object> condition) {
        return OperLog.dao.findOperLogListForPaginate(operLog, page, rows, sort, order, condition);
    }

    /**
     * 增加系统操作日志
     */
    public void addOperLog(String oprContent, boolean oprResult, String cause, HttpServletRequest request) {
        OperLog operLog = new OperLog();
        operLog.setOprId(Identities.uuid2());
        operLog.setOprContent(oprContent);
        operLog.setOprResult(oprResult ? 0 : 1);
        operLog.setCause(cause);
        //operLog.setCreatets(DateUtil.getDateFormatter());
        operLog.setAddress(IPUtils.getIpAddr(request));

        //User user = (User) request.getSession().getAttribute(SystemConst.SESSION_USER);
        User user = UAPSSOHelper.getUserByUapSessionId(request);
        if (null != user) {
            operLog.setUserId(user.getUserId());
            operLog.setUserName(user.getUserName());
            operLog.save();
        }
    }

    /**
     * 增加系统操作日志
     */
    public void addOperLog(String oprContent, String bizContent, boolean oprResult, String cause, HttpServletRequest request) {
        OperLog operLog = new OperLog();
        operLog.setOprId(Identities.uuid2());
        operLog.setOprContent(oprContent + " [" + bizContent + "]");
        operLog.setOprResult(oprResult ? 0 : 1);
        operLog.setCause(cause);
        //operLog.setCreatets(DateUtil.getDateFormatter());
        operLog.setAddress(IPUtils.getIpAddr(request));

        //User user = (User) request.getSession().getAttribute(SystemConst.SESSION_USER);
        User user = UAPSSOHelper.getUserByUapSessionId(request);
        if (null != user) {
            operLog.setUserId(user.getUserId());
            operLog.setUserName(user.getUserName());
            operLog.save();
        }
    }

    public void addOperLog2(String oprContent, String policyName, String bizContent, boolean oprResult, String cause, HttpServletRequest request) {
        OperLog operLog = new OperLog();
        operLog.setOprId(Identities.uuid2());
        operLog.setOprContent(oprContent + "  " + policyName + " : [" + bizContent + "]");
        operLog.setOprResult(oprResult ? 0 : 1);
        operLog.setCause(cause);
        //operLog.setCreatets(DateUtil.getDateFormatter());
        operLog.setAddress(IPUtils.getIpAddr(request));

        //User user = (User) request.getSession().getAttribute(SystemConst.SESSION_USER);
        User user = UAPSSOHelper.getUserByUapSessionId(request);
        if (null != user) {
            operLog.setUserId(user.getUserId());
            operLog.setUserName(user.getUserName());
            operLog.save();
        }
    }

    public OperLog getInstance() {
        return OperLog.dao;
    }
}
